<?php

namespace app\admin\controller;

use app\admin\model\RoomModel;
use app\admin\model\SchoolModel;
use app\admin\model\StudentModel;
use cmf\controller\AdminBaseController;

use think\Db;
use think\db\Query;

class StudentController extends AdminBaseController
{

    public function index()
    {
        $name = $this->request->param('name');
        $school_id = $this->request->param('school_id');//接受school_id;
        $room_id = $this->request->param('room_id');
        $room = Db::name('room')->where('sid',$school_id)->select();
        $school = Db::name('school')->select();
        $student = Db::name('student')
            ->join('cmf_room', 'cmf_student.cid=cmf_room.id', 'LEFT')
            ->join('cmf_school', 'cmf_room.sid=cmf_school.id', 'LEFT')
            ->field('cmf_school.name as school_name,cmf_room.name as room_name,cmf_student.*')
            ->where(function (Query $query) use ($name,$school_id,$room_id){
               if($name){
                   $query -> where('cmf_student','like',"%$name%");
               }
               if ($room_id){
                   $query ->where('cmf_room.id',"=","$room_id");
               }
               if ($school_id){
                   $query ->where('cmf_school.id',"=","$school_id");
               }
            })
            ->paginate(3,false,['$query'=>request()->param()]);
            $page = $student->render();

        return view('index',[
           'student'=>$student,
           'school'=>$school,
           'room'=>$room,
           'page'=>$page,
           'room_id'=>$room_id,
           'school_id'=>$school_id
        ]);
    }
    public function add()
    {
        $school = SchoolModel::all();//获取学校的全部数据
        $room = RoomModel::all();//获取班级的全部数据
        return view('add', ['school' => $school, 'room' => $room]);//渲染页面；接收返回的值
    }

    public function addPost()
    {
        //判断提交类型
        if ($this->request->isPost()) {
            $data = input();
            //halt($data);
            if (empty($data['name'])) {//判断姓名不能为空
                return $this->error('姓名不能为空');
            }
            unset($data['photo_name']);//删除图片变量
            unset($data['school_id']);
            $data['create_time']=date('Y-m-d H:i:s',time());
            $data['updata_time']=date('Y-m-d H:i:s',time());
            $add = Db::name('student')->insert($data);//获取数据中的id，添加数据
            if ($add) {//判断是否添加成功
                $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
        }
        }

    public function getRoombySchool()
    {
        //
        $schoolId = input('school_id');//获取当前school_id
        $room = RoomModel::where('sid', $schoolId)->select();
        //查询班级列表中的school_id
        if ($room) {
            $option = ["<option value=''>--请选择班级--</option>"];//默认学校值
            foreach ($room as $v) {//循环
                $opt = "<option value='" . $v['id'] . "'>" . $v['name'] . "</option>";
                array_push($option, $opt);
            }
            return $option;
        } else {
            return "<option value=''>暂无数据</option>";
        }
    }

    public function edit($id)
    {
        $school=Db::name('school')->select();
        $student=Db::name('student')->get($id);
        $schoolId=Db::name('room')->where('id',$student["cid"])->value('sid');
        //查询班级表中的school_id的值
        $room =Db::name('room')->where('sid',$schoolId)->select();
        return $this->fetch('edit',[
            'student'=>$student,
            'school_id'=>$schoolId,
            'school'=>$school,
            'room'=>$room
        ]);
    }
    public function editPost()
    {
        //获取edit.html提交过来的值
        $data = input();
        //halt($data);
        //更新创建时间
        $data['create_time'] = date('Y-m-d H:i:s',time());
        $data['updata_time'] = date('Y-m-d H:i:s',time());
        //销毁photo_name变量
        unset($data['photo_name']);
        unset($data['school_id']);
        //从数据库获取这条数据的id，并更新数据
        $edit = Db::name('student')->update($data, ['id' => $data['id']]);
        //获取是否编辑成功
        if ($edit){
            //设置成功后跳转页面的地址，成功提示且跳转
            $this->success('修改成功');
        }else{
            //错误页面的默认跳转页面是返回前一页，失败，提示
            $this->error('修改失败');
        }

    }
    public function chengji($id){
        $result = Db::name('result')
            ->join('cmf_subject','cmf_result.sub_id=cmf_subject.id')
            ->join('cmf_student','cmf_result.stu_id=cmf_student.id')
            ->join('cmf_room','cmf_student.cid=cmf_room.id')
            ->field('cmf_subject.name as subject_name,cmf_room.name as room_name , 
            cmf_result.*,cmf_student.name as student_name')
            ->where('cmf_result.id',$id)
            ->select();
        return $this->fetch('result',['result'=>$result]);
    }

    public function delete($id){
        $del=Db::name('student')->delete($id);
        if ($del){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}
